package cn.dceast.former.controller;

import cn.dceast.common.data.RtnData;
import cn.dceast.common.utils.ExportInternalUtil;
import cn.dceast.common.utils.ModelUtil;
import cn.dceast.former.model.EyeImg;
import cn.dceast.former.service.EyeImgSettingService;
import cn.dceast.situation.model.Record;
import cn.dceast.situation.service.SituationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.xssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouxiaa on 2017-07-18.
 * 展示图形设置和结果设置
 * */
@Controller
@RequestMapping("/eyeImgSettingController")
@Api(tags = {"eyeImgSettingController"})
public class EyeImgSettingController {
    public static final Logger logger = LoggerFactory.getLogger(EyeImgSettingController.class);

    @Autowired
    private EyeImgSettingService eyeImgSettingService;

    @Autowired
    private SituationService situationService;

    /**
     * 添加图形设置

     * @return
     */
    @ApiOperation(value = "添加图形设置", notes = "添加图形设置")
    @RequestMapping(value = "/addSetting.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData addSetting(EyeImg eyeImg, HttpSession session){
        //eyeImg.setCreateby((Integer) session.getAttribute("userid"));
        int isHave = eyeImgSettingService.checkHaveSettingName(eyeImg);
        if (isHave>0){
            return RtnData.fail("图表名称已存在");
        }else{
            int count = eyeImgSettingService.addSetting(eyeImg);
            if (count>0){
                return RtnData.ok("添加成功");
            }else{
                return RtnData.fail("添加失败");
            }
        }
    }
    /**
     * 编辑图形设置
     * @return
     */
    @ApiOperation(value = "编辑图形设置", notes = "编辑图形设置")
    @RequestMapping(value = "/updateSetting.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData updateSetting(EyeImg eyeImg, HttpSession session){
        eyeImg.setUpdateby((Integer) session.getAttribute("userid"));
        int isHave = eyeImgSettingService.checkHaveSameName(eyeImg);
        if (isHave>0){
            return RtnData.fail("图表名称已存在");
        }else {
            int count = eyeImgSettingService.updateSetting(eyeImg);
            if (count > 0) {
                return RtnData.ok("编辑成功");
            } else {
                return RtnData.fail("编辑失败");
            }
        }
    }
    /**
     * 删除图形设置
     * @return
     */
    @ApiOperation(value = "删除图形设置", notes = "删除图形设置")
    @RequestMapping(value = "/deleteSetting.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData deleteSetting(Integer id, HttpSession session){
        int count = eyeImgSettingService.deleteSetting(id);
        if (count>0){
            return RtnData.ok("删除成功");
        }else{
            return RtnData.ok("删除失败");
        }
    }
    /**
     * 查询图形设置
     * @return
     */
    @ApiOperation(value = "查询图形设置", notes = "查询图形设置")
    @RequestMapping(value = "/selectAllSetting.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData selectAllSetting(Integer modelId,HttpSession session){
        List<Map> list = eyeImgSettingService.selectAllSetting(modelId);
        return RtnData.ok(list);
    }
    /**
     * 删除图形设置详情
     * @return
     */
    @ApiOperation(value = "查询图形设置详情", notes = "查询图形设置详情")
    @RequestMapping(value = "/detail.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData detail(@RequestParam  Integer id,HttpSession session){
        EyeImg eyeImg = eyeImgSettingService.selectSettingById(id);
        return RtnData.ok(eyeImg);
    }
    /**
     * 获取图形所需的统计数据
     * @return
     */
    @ApiOperation(value = "获取图形所需的统计数据", notes = "获取图形所需的统计数据")
    @RequestMapping(value = "/getImgDataList.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData getImgDataList(@RequestParam  Integer modelId,@RequestParam  String tableName,HttpSession session){
        List<Map> dataList = eyeImgSettingService.getImgDataList(modelId,tableName);
        return RtnData.ok(dataList);
    }
    /**
     * 获取图形所需的统计数据
     * @return
     */
    @ApiOperation(value = "获取图形所需的统计数据", notes = "获取图形所需的统计数据")
    @RequestMapping(value = "/getResultImgDataList.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData getResultImgDataList(@RequestParam  Integer modelId,@RequestParam  String tableName,@RequestParam Boolean isRecorded,HttpSession session){
        List<Map> dataList = eyeImgSettingService.getImgDataList(modelId,tableName);
        Record record = new Record();
        record.setE_model_id(modelId);
        record.setE_res_table_name(tableName);
        record.setE_user_id((Integer) session.getAttribute("userid"));
        if(isRecorded){
            int count = situationService.addRecord(record);
        }
        return RtnData.ok(dataList);
    }


}
